package com.leyou.sms.listener;

import com.leyou.sms.utils.SendMsgUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.leyou.common.constants.MQConstants.Exchange.SMS_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.Queue.SMS_VERIFY_CODE_QUEUE;
import static com.leyou.common.constants.MQConstants.RoutingKey.ITEM_DOWN_KEY;
import static com.leyou.common.constants.MQConstants.RoutingKey.VERIFY_CODE_KEY;

@Slf4j
@Component
public class SendMsgListener {

    @Autowired
    private SendMsgUtils sendMsgUtils;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = SMS_VERIFY_CODE_QUEUE, durable = "true"),
            exchange = @Exchange(value = SMS_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = VERIFY_CODE_KEY
    ))
    public void sendCheckMsgListener(Map<String,String> checkCodeMap){
        try {
            if(checkCodeMap == null){
                return;
            }
            String phone = checkCodeMap.get("phone");
            String code = checkCodeMap.get("code");
            sendMsgUtils.sendCheckCodeMsg(phone,code);
        } catch (Exception e) {
            log.error("【SMS服务】短信验证码发送失败",e);
        }
    }

}
